using System;
using System.Collections.Generic;
using com.youzu.warh.protocol;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.AI
{
    [Serializable]
    public class WeaponSystem
    {
        public Dictionary<int, Weapon> WeaponMap = new Dictionary<int, Weapon>(10);
        [SerializeField]
        public List<Weapon> WeaponList = new List<Weapon>(10);
        public Weapon EquippedWeapon; //当前武器栏位装备
        public Weapon EquippedGrenade;//当前手雷栏位装备

        public Weapon HandleWeapon;//当前持有武器
        public int SelectedWeaponId => HandleWeapon != null ? HandleWeapon.Id : 101;

        /// <summary>
        /// 清理武器
        /// </summary>
        public void ClearWeapons()
        {
            WeaponMap.Clear();
            WeaponList.Clear();
            EquippedWeapon = null;
            EquippedGrenade = null;
            HandleWeapon = null;
        }

        /// <summary>
        /// 初始化武器
        /// </summary>
        public void InitWeapon(PbAIPropertyDataConfig config, bool isPlayer)
        {
            AddWeapon(101);//空手

            int grenadeWeaponId = 0;
            PbWeaponDataConfig weaponDataConfig;
            int weaponId, bulletCount;
            for (int i = 0; i < config.weapon_list.Count / 2; ++i)
            {
                weaponId = config.weapon_list[i * 2];
                bulletCount = config.weapon_list[i * 2 + 1];
                AddWeapon(weaponId, bulletCount);
                
                // 手雷
                weaponDataConfig = LibertyConfigManager.GetInstance().GetWeaponDataConfig(weaponId);
                if (weaponDataConfig.battle_state == (int)BattleState.Bomb)
                {
                    grenadeWeaponId = weaponId;
                }
            }

            if (isPlayer)
            {
                // 玩家
                EquipWeapon(101);
                if (grenadeWeaponId > 0)
                {
                    EquipWeapon(grenadeWeaponId);
                }
            }
            else
            {
                // npc
                EquipWeapon(101);
            }
        }
        
        /// <summary>
        /// 添加并装备武器
        /// </summary>
        /// <param name="weaponId"></param>
        /// <param name="bulletNum"></param>
        public void EquipWeapon(int weaponId)
        {
            Weapon weapon = null;
            if (!WeaponMap.TryGetValue(weaponId, out weapon))
            {
                Debug.LogError($"尝试装备未拥有的武器，id为{weaponId}");
                weapon = AddWeapon(weaponId);
            }
            
            if (weapon.Config.battle_state == (int)BattleState.Bomb)
            {
                EquippedGrenade = weapon;
            }
            else
            {
                EquippedWeapon = weapon;
            }
        }

        public bool IsSelectedGrenade()
        {
            return HandleWeapon.Config.battle_state == (int)BattleState.Bomb;
        }

        public Weapon AddWeapon(int weaponId, int bulletNum = -1)
        {
            Weapon weapon = null;
            if (!WeaponMap.TryGetValue(weaponId, out weapon))
            {
                PbWeaponDataConfig weaponDataConfig = LibertyConfigManager.GetInstance().GetWeaponDataConfig(weaponId);
                int maxBullet = 0;
                if (weaponDataConfig.bullet_limit == -1 || (0 <= bulletNum && bulletNum <= weaponDataConfig.bullet_limit))
                {
                    // 无子弹上限 or 有子弹上限且子弹数量小于等于上限
                    maxBullet = bulletNum;
                }
                else
                {
                    maxBullet = weaponDataConfig.bullet_limit;
                }
                weapon = new Weapon()
                {
                    Capacity = weaponDataConfig.magazine_capacity,
                    MaxBullet = maxBullet,
                    CurrentBullet = 0,
                    Config = weaponDataConfig,
                    Id = weaponId,
                    IsGrenade =  weaponDataConfig.battle_state == (int)BattleState.Bomb
                };
                WeaponMap.Add(weaponId, weapon);
                WeaponList.Add(weapon);
                // 上弹
                ReloadWeapon(weaponId);
            }
            else
            {
                // 有子弹上限的武器，只增加子弹数量
                if (bulletNum > 0 && weapon.MaxBullet >= 0)
                {
                    if (weapon.Config.bullet_limit == -1)
                    {
                        // 无子弹上限
                        weapon.MaxBullet += bulletNum;
                    }
                    else
                    {
                        // 有子弹上限
                        weapon.MaxBullet = Mathf.Min(weapon.MaxBullet + bulletNum, weapon.Config.bullet_limit);
                    }
                    
                    if (weapon.CurrentBullet == 0)
                    {
                        // 上弹
                        ReloadWeapon(weaponId);
                    }
                }
            }

            return weapon;
        }


        public void ChangeWeapon(int id)
        {
            if (WeaponMap.TryGetValue(id,out var weapon))
            {
                if (HandleWeapon!=null && HandleWeapon.Id == id)
                {
                    return;
                }
                HandleWeapon = weapon;
            }
            else
            {
                UnityEngine.Debug.LogError($"所选id为{id}的武器，背包里不存在");
            }
        }

        public void ReloadWeapon(int weaponId)
        {
            Weapon weapon;
            if (WeaponMap.TryGetValue(weaponId, out weapon))
            {
                if (weapon.MaxBullet == -1)
                {
                    weapon.CurrentBullet = weapon.Capacity;
                }
                else
                {
                    int reloadCount = (weapon.MaxBullet >= weapon.Capacity) ? weapon.Capacity : weapon.MaxBullet;
                    weapon.CurrentBullet = reloadCount;
                }
            }
        }

        public void UseWeapon(int weaponId)
        {
            Weapon weapon;
            if (WeaponMap.TryGetValue(weaponId, out weapon))
            {
                if (weapon.CurrentBullet > 0)
                {
                    weapon.CurrentBullet--;
                }
                if (weapon.MaxBullet > 0)
                {
                    weapon.MaxBullet--;
                }
            }
        }

        public void SwitchEmpty()
        {
            EquippedWeapon = WeaponMap[101];
            HandleWeapon = EquippedWeapon;
        }

        /// <summary>
        /// 获取优先级最高的可用武器id
        /// </summary>
        /// <returns></returns>
        public int GetCanUseFirstPriorityWeaponId(bool isGrenade)
        {
            int weaponId = 0;
            int priority = int.MinValue;
            foreach (var weapon in WeaponList)
            {
                if(weapon.IsGrenade == isGrenade && weapon.Config.priority > priority && (weapon.MaxBullet > 0 || weapon.MaxBullet == -1))
                {
                    weaponId = weapon.Id;
                    priority = weapon.Config.priority;
                }
            }

            return weaponId;
        }

        public int GetRestBulletCount(int weaponId)
        {
            if (WeaponMap.TryGetValue(weaponId, out var weapon))
            {
                return weapon.CurrentBullet;
            }
            return 0;
        }

        public int GetMaxBulletCount(int weaponId)
        {
            if (WeaponMap.TryGetValue(weaponId, out var weapon))
            {
                return weapon.MaxBullet;
            }
            return 0;
        }
        
        public int GetCapacity(int weaponId)
        {
            if (WeaponMap.TryGetValue(weaponId, out var weapon))
            {
                return weapon.Capacity;
            }
            return 0;
        }

        public bool IsEmpty()
        {
            if (EquippedWeapon == null)
            {
                return true;
            }
            return EquippedWeapon.Id == 101;
        }

        public List<Weapon> GetWeapons()
        {
            return WeaponList;
        }
        
    }
}